angular.module('start.common.password.ctrl', [])

.controller('PasswordCtrl', function($scope, $rootScope, $$AuthService, $ionicLoading, $interval, $location) {
    console.log("PasswordresetCtrl");

    $('.user_login_message').unbind();
    $scope.passwordData = {
      telephone: "",
      password: "",
      validateCode: "",
    };

    $scope.tipmessage = "获取验证码";
    $scope.times = 60; /*倒计时秒数*/


    $scope.registerSubmit = function() {

      if ($scope.validFormRegister()) {
        $$AuthService.modifyPassword($scope.passwordData)
          .then(function(res) {
            // 修改密码成功
            //更新
            // $$UserDataService.updateUserData(res.result);
            $location.path("login");
            $ionicLoading.show({
              template: "修改成功",
              noBackdrop: true,
              duration: 1500
            });
          });
      } else {
        // 校验失败
      }


    }; // registerSubmit();

    // 发送验证码
    $scope.sendCode = function() {
      // 手机校验
      if (!checkPhone($scope.passwordData.telephone)) {
        $ionicLoading.show({
          template: '请输入正确的手机号码',
          noBackdrop: true,
          duration: 1500
        });
        return;
      }
      console.log("sendCode。。。。");
      console.log($scope.times);
      if ($scope.times == 60) {
        $scope.times = 61;
        $scope.tipmessage = "发送中";
        //发送验证码sendCode()方法，接收$scope.passwordData.telephone参数
        $$AuthService.sendRegisterCode($scope.passwordData.telephone)
          .then(function(res) {
            console.log(res);
            if (res.errCode == 203) { /*等于200则进入定时器*/
              $scope.times = 59;

              var timer = $interval(function() { /*定义一个计时器*/
                if ($scope.times <= 0) {
                  $scope.times = 59; /*初始秒数*/
                  $scope.tipmessage = "重新发送";
                  $interval.cancel(timer); /*清除定时*/
                } else {
                  $scope.tipmessage = "已发送 " + $scope.times--; /*发送*/
                  if ($scope.times) {}
                }
              }, 1000);

            } else {
              $ionicLoading.show({
                template: res.msg,
                noBackdrop: true,
                duration: 1500
              });
              $scope.times = 60;
            }
          }, function(res) {
            console.log(res);
            $ionicLoading.show({
              template: res.msg,
              noBackdrop: true,
              duration: 1500
            });
                  $scope.tipmessage = "重新发送";
            $scope.times = 60;
          });
      }
    };


    $scope.validFormRegister = function() {
      // 手机校验
      if (!checkPhone($scope.passwordData.telephone)) {
        $ionicLoading.show({
          template: '请输入正确的手机号码',
          noBackdrop: true,
          duration: 1500
        });
        return false;
      }
      // 密码校验
      if (!checkPassword($scope.passwordData.password)) {
        $ionicLoading.show({
          template: '请输入6~12位的密码',
          noBackdrop: true,
          duration: 1500
        });
        return false;
      }
      if ($scope.passwordData.validateCode == "") {
        $ionicLoading.show({
          template: '验证码不能为空',
          noBackdrop: true,
          duration: 1500
        });
        return;
      }
      return true;
    };

  });
